define(["require",'config/appregister',"business/special/service/http","jquery","business/services/detailNavComp","business/home/directives/editorDirective","business/services/filter"], function(require, app, httpServices){
	app.controller('specialQuestionCtrl',function($scope, $rootScope, $state, $stateParams){
		$scope.questionType='all';
		var solve=0;
		$scope.nullBgFlag=false;
		$scope.editorContent='';
		$scope.createQuestionFlag=false;
		$scope.answerFlag=false;
		$scope.submitAnswer=function(qustionId,userId){
			if($rootScope.userLoginInfo.data.userId == userId){
				alert("自己不能回答自己的问题");
				return;
			}
			if($scope.editorContent.length > 4000){
				alert("最多输入2000个字符，您超过了限制，请做删减");
				return;
			}
			if($scope.editorContent.length < 9){
				alert("回答不能为空")
				return;
			}
			var req = {
				type:'POST',
				uri:'addReply',
				data:{
					postbarId: qustionId,
					content: $scope.editorContent,
					seed:localStorage.seed,
					authToken: localStorage.authToken
				}
			};
			$.ajax({
				type:'post',
				dataType:'json',
				data:req.data,
				url:"http://"+allUrl+":8086/elink_postbar/api/reply/addReply",
				success:function(data) {
					if(!data)return;
					$scope.$apply(function(){
						if(data.code==0) {
							alert("快速回复成功");
							getList(type,catalogId,$scope.pageNO,15);
						}else {
							alert(data.msg);
						}
					});
				},
				error:function() {
					alert("快速回复请求失败");
				}
			});
		};
		if(!angular.isUndefined($stateParams.catalogId)) {
			var catalogId=$stateParams.catalogId;
		}
		if(!angular.isUndefined($stateParams.type)) {
			var type=$stateParams.type;
		}

		if(catalogId==1){
			$scope.bannerSrc='./them/special/banner1.jpg';
		}else if(catalogId==2){
			$scope.bannerSrc='./them/special/banner2.jpg';
		}else if(catalogId==3){
			$scope.bannerSrc='./them/special/banner3.jpg';
		}else if(catalogId==4){
			$scope.bannerSrc='./them/special/banner4.jpg';
		}else if(catalogId=='5e199b0c353e476a90481464a4afb0bb'){
			$scope.bannerSrc='./them/special/banner5.jpg';
		}

		$scope.openEdit=function(item){
			if($rootScope.userLoginInfo.logged) {
				var flag=item.flag;
				for(var i in $scope.dataList){
					$scope.dataList[i].flag=false;
					$scope.dataList[i].html='快速回答';
				}
				if(flag){
					item.flag=false;
					item.html='快速回答';
				}else{
					item.flag=true;
					item.html='取消回答'
				}
			}else {
				$scope.answerFlag=true;
			}
		};
		//获取首页问答信息
		getList(type,catalogId,1,15);
		function getList(type,id,pageNum,num){
			var req = {
				type:"GET",
				uri:'getListByCatalogId',
				data:{
					type:type,
					catalogId:id,
					pageNo:pageNum,
					pageSize:num,
					solve:solve
				}
			};
			httpServices.specialHttpService(req).success(function(data){
				if(!data)return;
				$scope.$apply(function(){
					if(data.code == 0) {
						$scope.category=data.data.category;
						$scope.title=$scope.category.subjectName+'——'+$scope.category.subtitle;
						$scope.data=data.data;
						$scope.pageNO=data.data.Pagination.pageNo;
						$scope.dataList=data.data.list;
						if(data.data.list.length==0){
							$scope.nullBgFlag=true;
							return;
						}else{
							$scope.nullBgFlag=false;
						}
						for(var item in $scope.dataList){
							$scope.dataList[item].flag=false;
							$scope.dataList[item].html='快速回答';
							if($scope.dataList[item].solve==1){
								$scope.dataList[item].solveHtml='待解决'
							}else{
								$scope.dataList[item].solveHtml='已解决'
							}
						}
						paginationConf();
					}else {
						console.log(data.msg)
					}
				});
			}).error(function(){
				console.log("分页请求失败");
			});
		};

		function paginationConf(){
			$scope.paginationConf = {
				currentPage:$scope.pageNO ,
				totalItems: $scope.data.Pagination.totalCount,
				//每页预览条数
				itemsPerPage: 15,
				//分页总数
				numberOfPages: Math.ceil($scope.data.Pagination.totalCount / 15),
				// 实际分页长度
				pagesLength: 5,
				perPageOptions: [15, 50, 45, 60, 75],
				onChange:function(currentPage) {
					if(currentPage) {
						$scope.paginationConf.currentPage= currentPage;
					}
					$scope.list = [];
					$scope.pageNO = $scope.paginationConf.currentPage;
					getList(type,catalogId,$scope.pageNO,15);
				}
			};
		}

		$scope.gotoiask = function() {
			if($rootScope.userLoginInfo.logged) {
				$state.go("eng.iask");
			}else {
				$scope.createQuestionFlag=true;
			}
		};

		$scope.solve=function(){
			$scope.questionType='solve';
			solve=1;
			getList(type,catalogId,1,15);
		}
		$scope.solved=function(){
			$scope.questionType='solved';
			solve=2;
			getList(type,catalogId,1,15);
		}
		$scope.all=function(){
			$scope.questionType='all';
			solve=0;
			getList(type,catalogId,1,15);
		}

		$scope.closeTipBox=function(str){
			if(str=='answer'){
				$scope.answerFlag=false;
			}else{
				$scope.createQuestionFlag=false;
			}
		}
	})
});